package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.ResourceService;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleResourceMapper roleResourceMapper;

    /*
     * 条件分页查询——角色表
     */
    @Override
    public PageResponse<RoleVo> findRoleVoPage(RoleDto roleDto, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<List<Role>> list = roleMapper.selectPage(roleDto);
        //利用已有方法将数据转换成PageResponse对象
        PageResponse<RoleVo> pageResponse = PageResponse.of(list, RoleVo.class);
        return pageResponse;
    }

    /**
     * 保存角色
     *
     * @param roleDto 角色DTO对象
     * @return RoleVo
     */
    @Override
    public void createRole(RoleDto roleDto) {
        //表对应的实体类是Role，需要转换为Role对象
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.insert(role);
    }

    /*
     * 根据角色id查询选中的资源数据
     */
    @Override
    public Set<String> findCheckedResources(Long roleId) {
        return roleResourceMapper.selectResourceNoByRoleId(roleId);
    }

    /*
     * 角色修改
     */
    @Override
    @Transactional//因为操作多个表，所以要保证事务一致性
    public Boolean updateRole(RoleDto roleDto) {

        //1.对象拷贝，因为表对应的是Role对象
        Role role = BeanUtil.toBean(roleDto, Role.class);

        //2.修改角色表
        roleMapper.updateByPrimaryKeySelective(role);

        //3.判断是否修改角色对应的资源数据，如果为空，则表示只修改角色，则返回 true
        if(ObjectUtil.isEmpty(roleDto.getCheckedResourceNos())){
            return true;
        }

        //4.如果不为空，则需要删除角色与资源关联表中有关角色的数据，重新插入
        roleResourceMapper.deleteRoleResourceByRoleId(role.getId());

        //5.保存角色资源中间信息——关联数据
        List<RoleResource> roleResourceList= Lists.newArrayList();
        Arrays.asList(roleDto.getCheckedResourceNos()).forEach(n->{
            RoleResource roleResource = RoleResource.builder()
                    .roleId(role.getId())
                    .resourceNo(n)
                    .dataState(SuperConstant.DATA_STATE_0)
                    .build();
            roleResourceList.add(roleResource);
        });

        //6.判断资源集合是否为空，为空则结束请求
        if(ObjectUtil.isEmpty(roleResourceList)){
            return true;
        }

        //7.资源集合不为空，批量保存角色资源关联信息
        roleResourceMapper.batchInsert(roleResourceList);

        return true;
    }

    /*
     * 删除角色
     */
    @Override
    @Transactional
    public void deleteRoleById(Long roleId) {
        //删除角色与菜单关联信息
        roleResourceMapper.deleteRoleResourceByRoleId(roleId);
        //删除角色基本信息
        roleMapper.deleteByPrimaryKey(roleId);
    }

    /***
     *  角色下拉框
     * @return
     */
    @Override
    public List<RoleVo> initRoles() {
        List<Role> roleList = roleMapper.initRoles();
        return BeanUtil.copyToList(roleList,RoleVo.class);
    }
}
